package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.CollectionBehaviorDto;
import com.heima.behavior.pojos.ApCollectionBehavior;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApTheadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author shitou
 * @className ApCollectionBehaviorImpl
 * @description: TODO
 */
@Service
@Slf4j
public class ApCollectionBehaviorImpl implements ApCollectionBehaviorService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 收藏行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        log.info("收藏行为已执行");
        // 校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Long articleId = dto.getEntryId();
        if (articleId == null || articleId < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 用户未登录
        ApUser apUser = ApTheadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        // 游客需要进行登录，才能进行用户行为
        if (userId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String userCollection = "USER_COLLECTION_" + articleId;
        // 查询redis中当前用户是否点过喜欢
        Boolean userCollectionFlag = stringRedisTemplate.opsForHash().hasKey(userCollection, userId + "");
        //0 收藏 1 取消收藏
        short type = dto.getOperation();
        //  用户没有点过喜欢,进行点不喜欢行为
        if (!userCollectionFlag && type == 0) {
            // 点收藏成功存入redis
            stringRedisTemplate.opsForHash().put(userCollection, userId + "", "1");
            // 存入mongo
            ApCollectionBehavior apCollectionBehavior = new ApCollectionBehavior();
            apCollectionBehavior.setCollectionTime(new Date());
            apCollectionBehavior.setUserId(apUser.getId().longValue());
            apCollectionBehavior.setArticleId(dto.getEntryId());
            apCollectionBehavior.setType(type);
            mongoTemplate.insert(apCollectionBehavior);
        }
        // 用户取消收藏，删除redis mongo中数据
        if (userCollectionFlag && type == 1) {
            stringRedisTemplate.opsForHash().delete(userCollection, userId+ "");
            Query query = Query.query(Criteria.where("userId").is(apUser.getId()).and("articleId").is(articleId));
            mongoTemplate.remove(query, ApCollectionBehavior.class);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
